//
//  HMEmoticonModel.swift
//  04-表情键盘
//
//  Created by Admin on 16/9/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMEmoticonModel: NSObject, NSCoding {
    // MARK: - 属性
    /// 表情包 文件夹 名称
    var id: String
    
    /// 表情名称, 用于网络传输
    var chs: String?
    
    /// 表情图片名称, 在手机上显示表情图片
    var png: String? {
        didSet {
            // 图片完整路径 = EmoticonBundlePath/表情包文件夹/png
            fullPngPath = bundlePath + "/" + id + "/" + png!
        }
    }
    
    /// png的完整路径 = bundlePath/表情包文件夹/png
    var fullPngPath: String?
    
    /// emoji 16进制字符串
    var code: String? {
        didSet {
            // 将 code 转成 emoji字符串
            let scanner = Scanner(string: code!)
            
            var result: UInt32 = 0
            scanner.scanHexInt32(&result)
            
            emoji = "\(Character(UnicodeScalar(result)!))"
        }
    }
    
    /// emoji字符串
    var emoji: String?
    
    // MARK: - 字典转模型
    init(id: String, dict: [String: String]) {
        self.id = id
        super.init()
        
        // KVC
        setValuesForKeys(dict)
    }
    
    // 字典的key在模型中找不到对应的属性
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
    
    /// 打印对象
    override var description: String {
        return "\n\t\t: 表情模型:chs:\(chs), png:\(png), code:\(code)"
    }
    
    required init?(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObject(forKey: "id") as? String ?? ""
        chs = aDecoder.decodeObject(forKey: "chs") as? String
        png = aDecoder.decodeObject(forKey: "png") as? String
        code = aDecoder.decodeObject(forKey: "code") as? String
        emoji = aDecoder.decodeObject(forKey: "emoji") as? String
        
//        fullPngPath = aDecoder.decodeObject(forKey: "fullPngPath") as? String
        // 重新拼接全路径,因为在模拟器上面重新运行沙盒的路径会变化pngPath会失效
        if let p = png {
            fullPngPath = bundlePath + "/" + id + "/" + p
        }
    }
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(id, forKey: "id")
        aCoder.encode(chs, forKey: "chs")
        aCoder.encode(png, forKey: "png")
        aCoder.encode(fullPngPath, forKey: "fullPngPath")
        aCoder.encode(code, forKey: "code")
        aCoder.encode(emoji, forKey: "emoji")
    }
    
    func toAttrString(font: UIFont) -> NSAttributedString {
        // 附件,图片可以作为附件,附件可以添加到属性文本
        let attachment = HMTextAttachment()
        attachment.image = UIImage(contentsOfFile: fullPngPath!)
        attachment.chs = chs

        // 获取文本的高度
        let height = font.lineHeight
        // 设置附件的大小
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)

        // 创建带附件的属性文本,并且添加font属性
        let attrString = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        attrString.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return attrString
    }
    
    /**
     根据表情名称查找表情模型
     - parameter name: 表情名称
     - returns: 表情模型
     */
    class func emoticonMode(withEmoticonName name: String) -> HMEmoticonModel? {
        // 去所有的表情包里面找
        for package in HMEmoticonManager.shared.packages {
            for emoticon in package.emoticons {
                if emoticon.chs == name {
                    // 找到了
                    return emoticon
                }
            }
        }
        
        return nil
    }
}
